﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using yt_.netcore3._1_webapi_les_jackson.Models;

namespace yt_.netcore3._1_webapi_les_jackson.Data
{
    public class CommandRepo:ICommanderRepo
    {
        private readonly CommanderContext _commanderContext;

        public CommandRepo(CommanderContext commanderContext)
        {
            _commanderContext = commanderContext;
        }

        public bool SaveChanges()
        {
            return (_commanderContext.SaveChanges()>0);
        }

        public IEnumerable<Command> GetAllCommands()
        {
            return _commanderContext.Commands.ToList();
        }

        public async Task<Command> GetCommandById(int id)
        {
            return await _commanderContext.Commands.FirstOrDefaultAsync(item => item.Id == id);
        }

        public async Task CreateCommand(Command cmd)
        {
            if (cmd == null)
            {
                throw new ArgumentNullException(nameof(cmd));
            }
            await _commanderContext.Commands.AddAsync(cmd);
            
        }

        public void UpdateCommand(Command cmd)
        {
            //Nothing
            if (cmd == null)
            {
                throw new ArgumentNullException(nameof(cmd));
            }

            _commanderContext.Commands.Update(cmd);
        }

        public void DeleteCommand(Command cmd)
        {
            _commanderContext.Commands.Remove(cmd);
        }
    }
}